home *** CD-ROM | disk | FTP | other *** search
- Imports Microsoft.Win32
-
- Public Class RegistryForm
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents Button1 As System.Windows.Forms.Button
- Friend WithEvents txtOut As System.Windows.Forms.TextBox
- Friend WithEvents Button2 As System.Windows.Forms.Button
- Friend WithEvents Button3 As System.Windows.Forms.Button
- Friend WithEvents Button4 As System.Windows.Forms.Button
- Friend WithEvents Button5 As System.Windows.Forms.Button
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.Button1 = New System.Windows.Forms.Button()
- Me.txtOut = New System.Windows.Forms.TextBox()
- Me.Button2 = New System.Windows.Forms.Button()
- Me.Button3 = New System.Windows.Forms.Button()
- Me.Button4 = New System.Windows.Forms.Button()
- Me.Button5 = New System.Windows.Forms.Button()
- Me.SuspendLayout()
- '
- 'Button1
- '
- Me.Button1.Location = New System.Drawing.Point(16, 16)
- Me.Button1.Name = "Button1"
- Me.Button1.Size = New System.Drawing.Size(128, 40)
- Me.Button1.TabIndex = 0
- Me.Button1.Text = "Check Word installation"
- '
- 'txtOut
- '
- Me.txtOut.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
- Or System.Windows.Forms.AnchorStyles.Left) _
- Or System.Windows.Forms.AnchorStyles.Right)
- Me.txtOut.Location = New System.Drawing.Point(160, 16)
- Me.txtOut.Multiline = True
- Me.txtOut.Name = "txtOut"
- Me.txtOut.ScrollBars = System.Windows.Forms.ScrollBars.Both
- Me.txtOut.Size = New System.Drawing.Size(344, 296)
- Me.txtOut.TabIndex = 1
- Me.txtOut.Text = ""
- Me.txtOut.WordWrap = False
- '
- 'Button2
- '
- Me.Button2.Location = New System.Drawing.Point(16, 64)
- Me.Button2.Name = "Button2"
- Me.Button2.Size = New System.Drawing.Size(128, 40)
- Me.Button2.TabIndex = 0
- Me.Button2.Text = "ADODB.Recordset info"
- '
- 'Button3
- '
- Me.Button3.Location = New System.Drawing.Point(16, 112)
- Me.Button3.Name = "Button3"
- Me.Button3.Size = New System.Drawing.Size(128, 40)
- Me.Button3.TabIndex = 0
- Me.Button3.Text = "List COM Components"
- '
- 'Button4
- '
- Me.Button4.Location = New System.Drawing.Point(16, 160)
- Me.Button4.Name = "Button4"
- Me.Button4.Size = New System.Drawing.Size(128, 40)
- Me.Button4.TabIndex = 0
- Me.Button4.Text = "Create keys and values"
- '
- 'Button5
- '
- Me.Button5.Location = New System.Drawing.Point(16, 208)
- Me.Button5.Name = "Button5"
- Me.Button5.Size = New System.Drawing.Size(128, 40)
- Me.Button5.TabIndex = 0
- Me.Button5.Text = "Delete keys and values"
- '
- 'RegistryForm
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(512, 317)
- Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button5, Me.Button4, Me.Button3, Me.Button2, Me.txtOut, Me.Button1})
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Name = "RegistryForm"
- Me.Text = "Registry and RegistryKey"
- Me.ResumeLayout(False)
-
- End Sub
-
- #End Region
-
- ' Define the RegistryKey objects for the registry hives.
- Dim regClasses As RegistryKey = Registry.ClassesRoot
- Dim regCurrConfig As RegistryKey = Registry.CurrentConfig
- Dim regCurrUser As RegistryKey = Registry.CurrentUser
- Dim regDynData As RegistryKey = Registry.DynData
- Dim regLocalMachine As RegistryKey = Registry.LocalMachine
- Dim regPerfData As RegistryKey = Registry.PerformanceData
- Dim regUsers As RegistryKey = Registry.Users
-
- Sub LogMessage(ByVal msg As String)
- txtOut.AppendText(msg & ControlChars.CrLf)
- End Sub
-
- ' Check whether Microsoft Word is installed on this computer,
- ' by searching the HKEY_CLASSES_ROOT\Word.Application key.
-
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim regWord As RegistryKey = regClasses.OpenSubKey("Word.Application")
- If regWord Is Nothing Then
- LogMessage("Microsoft Word isn't installed")
- Else
- LogMessage("Microsoft Word is installed")
- End If
- ' Always close Registry keys after using them.
- regWord.Close()
- End Sub
-
- ' retrieve the GUID of a com component
-
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- Dim clsid As String = GetCLSID("Adodb.Recordset")
- LogMessage("CLSID of ADODB.Recordset = " & clsid)
- End Sub
-
- ' Return the CLSID of a COM component, or "" if not found.
-
- Function GetCLSID(ByVal ProgId As String) As String
- ' Open the key associated to the ProgID.
- Dim regProgID As RegistryKey = Registry.ClassesRoot.OpenSubKey(ProgId)
- If Not (regProgID Is Nothing) Then
- ' If found, open the CLSID subkey.
- Dim regClsid As RegistryKey = regProgID.OpenSubKey("CLSID")
- If Not (regClsid Is Nothing) Then
- ' If found, get its default value. 2nd optional argument is the
- ' string tp be returned if the specified value doesn't exist.
- ' (Returns an Object that we must convert to a string.)
- GetCLSID = CStr(regClsid.GetValue(""))
- ' Always close registry keys.
- regClsid.Close()
- End If
- ' Always close registry keys.
- regProgID.Close()
- End If
- End Function
-
- ' display the list of all installed COM components
-
- Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
- ' NOTE: on most machines the list of COM components is too large for a TextBox
- ' and the output will be truncated.
- txtOut.Text = ""
- DisplayCOMComponents()
- End Sub
-
- ' Print ProgID, CLSID, and path of all the COM components
- ' installed on this computer.
- Sub DisplayCOMComponents()
- ' Open the HKEY_CLASSESROOT\CLSID key
- Dim regClsid As RegistryKey = Registry.ClassesRoot.OpenSubKey("CLSID")
-
- ' Iterate over all the subkeys.
- Dim clsid As String
- For Each clsid In regClsid.GetSubKeyNames
- ' Open the subkey.
- Dim regClsidKey As RegistryKey = regClsid.OpenSubKey(clsid)
-
- ' Get the ProgID (this is the default value for this key).
- Dim ProgID As String = CStr(regClsidKey.GetValue(""))
- ' Get the InProcServer32 key, that holds the DLL path.
- Dim regPath As RegistryKey = regClsidKey.OpenSubKey("InprocServer32")
- If regPath Is Nothing Then
- ' If not found, it isn't an in-process DLL server,
- ' let's see if it is an out-process EXE server.
- regPath = regClsidKey.OpenSubKey("LocalServer32")
- End If
- If Not (regPath Is Nothing) Then
- ' If either key has been found, retrieve its default value.
- Dim filePath As String = CStr(regPath.GetValue(""))
- ' Display all the relevant info gathered so far.
- LogMessage(ProgID & " " & clsid & " -> " & filePath)
- ' Always close open registry keys.
- regPath.Close()
- End If
- ' Always close open registry keys.
- regClsidKey.Close()
- Next
- End Sub
-
- ' create new registry keys
-
- Dim regSoftware As RegistryKey
- Dim regCompany As RegistryKey
- Dim regProduct As RegistryKey
-
- Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
- ' The following code snippets, taken together, add company/product
- ' keys under the HKEY_LOCALMACHINE\SOFTWARE key, as many Windows apps do.
-
- ' Open the HKEY_LOCALMACHINE\SOFTWARE key.
- regSoftware = Registry.LocalMachine.OpenSubKey("SOFTWARE", True)
-
- ' Add a key for the company name (or open it if it exists already).
- regCompany = regSoftware.CreateSubKey("VB2TheMax")
- ' Add another key for the product name (or open it if it exists already).
- regProduct = regCompany.CreateSubKey("VBMaximizer")
-
- ' Create three Values under the Product key.
- regProduct.SetValue("Path", "C:\VBMaximizer\Bin") ' a string value
- regProduct.SetValue("MajorVersion", 2) ' a numeric value
- regProduct.SetValue("MinorVersion", 1) ' a numeric value
-
- LogMessage("The \SOFTWARE\VB2TheMax key has been created")
- End Sub
-
- ' destroy the registry keys created previously
-
- Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
- ' Delete the three values just added.
- regProduct.DeleteValue("Path")
- regProduct.DeleteValue("MajorVersion")
- regProduct.DeleteValue("MinorVersion")
- ' Delete the Product key, after closing it.
- regProduct.Close()
- regCompany.DeleteSubKey("VBMaximizer")
- ' Delete the Company key, after closing it.
- regCompany.Close()
- regSoftware.DeleteSubKey("VB2TheMax")
-
- LogMessage("The \SOFTWARE\VB2TheMax key has been deleted")
- End Sub
- End Class
-